package cn.juque.systemservice.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.systemservice.domain.RoleFuncRelationDO;
import cn.juque.systemservice.entity.RoleFuncRelation;
import cn.juque.systemservice.entity.RoleInfo;
import cn.juque.systemservice.mapper.RoleFuncRelationMapper;
import cn.juque.systemservice.mapper.RoleInfoMapper;
import cn.juque.systemservice.service.IRoleInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>角色信息</li>
 * </ul>
 * @date 2022-11-03 23:57:34
 **/
@Service("roleInfoService")
public class RoleInfoServiceImpl implements IRoleInfoService {

    @Resource
    private RoleInfoMapper roleInfoMapper;

    @Resource
    private RoleFuncRelationMapper roleFuncRelationMapper;

    /**
     * 分页查询
     *
     * @param pageInfo pageInfo
     * @param roleInfo roleInfo
     * @return DataGrid
     */
    @Override
    public List<RoleInfo> listInfoWithPage(PageInfo pageInfo, RoleInfo roleInfo) {
        Page<RoleInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<RoleInfo> list = this.roleInfoMapper.queryInfoList(page, roleInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param roleInfo roleInfo
     * @return List
     */
    @Override
    public List<RoleInfo> listInfoWithoutPage(RoleInfo roleInfo) {
        return this.roleInfoMapper.queryInfoList(roleInfo);
    }

    /**
     * 检索功能列表
     *
     * @param roleId 角色ID
     * @return list
     */
    @Override
    public List<RoleFuncRelationDO> listFuncByRoleId(String roleId) {
        if (CharSequenceUtil.isEmpty(roleId)) {
            return CollUtil.newArrayList();
        }
        RoleFuncRelationDO roleFuncRelation = new RoleFuncRelationDO();
        roleFuncRelation.setRoleId(roleId);
        return this.roleFuncRelationMapper.queryFuncRelationList(roleFuncRelation);
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return RoleInfo
     */
    @Override
    public RoleInfo detail(String id) {
        return this.roleInfoMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param roleInfo roleInfo
     */
    @Override
    public void saveInfo(RoleInfo roleInfo) {
        boolean flag = SqlHelper.retBool(this.roleInfoMapper.insert(roleInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
    }

    /**
     * 保存功能关系
     *
     * @param roleId     角色ID
     * @param funcIdList 功能ID列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveFuncRelation(String roleId, List<String> funcIdList) {
        if (CharSequenceUtil.isEmpty(roleId) || CollUtil.isEmpty(funcIdList)) {
            return;
        }
        RoleFuncRelation roleFuncRelation = new RoleFuncRelation();
        roleFuncRelation.setRoleId(roleId);
        this.roleFuncRelationMapper.delete(new QueryWrapper<>(roleFuncRelation));
        funcIdList.forEach(t -> {
            RoleFuncRelation entity = new RoleFuncRelation();
            entity.setRoleId(roleId);
            entity.setMenuId(t);
            this.roleFuncRelationMapper.insert(entity);
        });
    }

    /**
     * 更新
     *
     * @param roleInfo roleInfo
     */
    @Override
    public void updateInfo(RoleInfo roleInfo) {
        boolean flag = SqlHelper.retBool(this.roleInfoMapper.updateById(roleInfo));
        if (!flag) {
            throw new AppException(MessageEnum.UPDATE_FAIL);
        }
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.roleInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
    }

}
